import './interfaces';

interface CountParam {
	criteria: object;
	query_mode: 1;
	sort: {};
	spaceid: string;
	// sql: string;
	systemid: string;
	tablename: string;
}

interface CountResult {
	query_mode: 1;
	spaceid: string;
	systemid: string;
	tablename: string;
	count: number;
}

function count(systemid: string, tablename: string, filter: object) {
	return {
		criteria: filter,
		query_mode: 1,
		sort: {},
		spaceid,
		systemid,
		tablename
	} as CountParam;
}

async function do_count(q: CountParam[]) {
	const data = await (send_msg('dbacc.count.intranet', q) as Promise<CountResult[]>);
	return data.map((d) => {
		return d.count;
	});
}

interface Message {
	// todo
	[field: string]: any;
}

async function atom(msg: Message, headers: IHeaders) {
	const p1 = count('@systemid', '@tablename1', msg);
	const p2 = count('@systemid', '@tablename2', msg);
	const [count1, count2] = await do_count([p1, p2]);
	return count1 > count2 ? 'gt' : 'lt';
}
